using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_BaseAttackState : PlayerStateBase
{
    private float lastTimeAttacked;
    private readonly int maxCombo = 3;
    private int comboCounter = 1;
    private bool comboAttackQueued;
    private const int FirstCombo = 1;
    public Player_BaseAttackState(Player player, StateMachine stateMachine, string animName) : base(player, stateMachine, animName)
    {

    }
    public override void Enter()
    {
        base.Enter();
        comboAttackQueued = false;
        //设置攻击速度
        SyncAttackSpeed();

        //限制连招招数
        LimitCombo(); 

        animator.SetInteger("ComboAttack", comboCounter);
        AppayAttackVelocity(); 
    }
    public override void Update()
    {
        base.Update();
        if (stateTimer < 0)
            player.SetVelocity(0, rb.velocity.y);

        if (input.Player.Attack.WasPressedThisFrame())
        {
            if (comboCounter < maxCombo)
                comboAttackQueued = true;
        }
        if (triggerCalled)
            HandleStateExit();
    }
    public override void Exit()
    {
        base.Exit();
        comboCounter++;
        lastTimeAttacked = Time.time;
    }
    private void HandleStateExit()
    {
        if (comboAttackQueued)
        {
            //该状态在转换这一帧一直为true，要先退出为false再设置为true
            animator.SetBool(animName, false);
            //等待下一帧
            player.EnterAttackStateWithDelay();
        }
        else
            stateMachine.ChangeState(player.idleState);
    }
    private void AppayAttackVelocity()
    {
        //设置攻击移动
        player.SetVelocity(player.GetAttackMovement()[comboCounter - 1].x * player.moveInput.x, player.GetAttackMovement()[comboCounter - 1].y);
        //攻击移动时间
        stateTimer = .1f;
    }
    private void LimitCombo()
    {
        //判断当前招数是否超过最大招数
        if (comboCounter > maxCombo || Time.time >= lastTimeAttacked + player.comboWindow)
        {
            comboCounter = FirstCombo;
        }
    }
}
